<?php 
namespace app\system\admin;
use app\system\admin\Admin;
use think\Db;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

/**
 *  @author Lin
 *  @time 2021/7/22
 */
class Xcxuser extends Admin
{
    protected function initialize()
    {
        parent::initialize();
        set_time_limit(0);
       
    }

    

    public function add(){
        if($this->request->isPost()){
            $list = $this->request->post();
            $type = $list['type'];
            unset($list['type']);
            if($type=='index'){
                // var_dump($list);exit;
                unset($list['id']);
                if(!$list['pic']){
                    return $this->error('请上传banner图');
                }
                
                $rs = Db::name("index_banner")->insert($list);
                // var_dump($list);
                // return $this->error($rs);
                if($rs){
                    return $this->success('添加成功');
                }
                else{
                    return $this->error('添加失败，请重试');
                }
                
            }
        }
        else{
            $type = input('type/s');
            $this->assign('type',$type);
            if($type=='index'){
                $template = $this->request->param('template', 'form');
            }
            return $this->fetch($template);
        }
    }

    public function edit(){
        if ($this->request->isPost()) {// 数据验证
            $list = $this->request->post();
            // var_dump($list);exit;
            if(!$list['id']){
                return $this->error('参数错误');
            }
            else{
                $id = $list['id'];
                $type = $list['type'];
                unset($list['id']);
                unset($list['type']);
                if($type=='index'){
                    if(!$list['pic']){
                        unset($list['pic']);
                    }
                    $rs = Db::name("index_banner")->where('id',$id)->update($list);
                    // var_dump($list);
                    // return $this->error($rs);
                    if($rs){
                        return $this->success('修改成功');
                    }
                    else{
                        return $this->error('修改失败，请重试');
                    }
                }
            }
        }
        $id = input("id/d");
        $type = input('get.type/s');
        if($type=="index"){
            $formData = Db::name("index_banner")->where("id",$id)->find();
            
            $tmp = "form";
        }
        $this->assign('formData', $formData);
        $this->assign('type', $type);
        $template = $this->request->param('template', $tmp);
        return $this->fetch($template);
    }
    function upload(){
        $file = request()->file('file');
        $info = $file->move(ROOT_PATH.'public/upload');
        // var_dump($info);exit;
        if($info) {
            $data['code'] = 0;
            $data['msg'] = $info->getSaveName();
            $data['type'] = $info->getExtension();//获取图片的类型
            $file_info = $info->getInfo();
            if($data['type'] == 'jpg' || $data['type'] == 'gif' || $data['type'] == 'png' || $data['type'] == 'jpeg'){
                Db::name('files')->insert([
                    'pic' => 'upload/'.$data['msg'],
                    'name'=>$file_info['name'],
                    'size'=>sprintf('%.2f',$file_info['size']/1024/1024)."M",
                    'type'=>$data['type'],
                    'ctime' => time()
                ]);
                return $this->success('提交成功');
            }
        }else{
            return $this->error('提交失败，请重试');
        }
    }

    function del(){
        $id = input("get.id/d");
        $type = input('get.type/s');
        // 删除类型
        if($type=='type'){
            if($id==0){
                return $this->error('总分类不能删除');
            }
            $rs = Db::name('product_type')->where('id',$id)->delete();
            if($rs){
                return $this->success('删除成功');
            }
            else{
                return $this->error('删除失败，请重试');
            }
        }
        elseif($type=="index"){
            $rs = Db::name('index_banner')->where('id',$id)->delete();
            if($rs){
                return $this->success('删除成功');
            }
            else{
                return $this->error('删除失败，请重试');
            }
        }
        
    }


    function index(){
        // 产品分类
        $gwhere = [];
        if ($this->request->isAjax()) {

            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $province = input('province');
            $city = input('city');
            if($province){
                $where[] = ['province','eq',$province];
            }
            if($city){
                $where[] = ['city','eq',$city];
            }
            $keyword = input('keyword');
            if(!empty($keyword)){
                $where[] = ['nickname','like','%'.$keyword.'%'];
            }
            $list = Db::name('user')->where($where)->page($page)->limit($limit)->order('jifen desc')->select();
            if($list){
                foreach($list as &$value){
                    $value['ctime'] = $value['ctime']?date('Y-m-d H:i:s',$value['ctime']):'';
                    if($value['status']==0){
                        $value['status'] = '正常';
                    }
                    else{
                        $value['status'] = '禁用';
                    }
                }
            }
            $data['data'] = $list;
            $data['count'] = Db::name('user')->where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }

        $pwhere[] = ['province','neq',''];
        $province = Db::name('user')->where($pwhere)->group('province')->field('province')->select();
        $li = '';
        if($province){
            foreach($province as $val){
                $li .= "<option value='".$val['province']."'>".$val['province']."</option>";
            }
        }
        $this->assign('li',$li);
        return $this->fetch();
    }

    function getCity(){
        $province = input('province');
        $where[] = ['province','eq',$province];
        $list = Db::name('user')->where($where)->group('city')->field('city')->select();
        $li = '<option value="0">请选择城市</option>';
        if($list){
            foreach($list as $value){
                $li .= "<option value='".$value['city']."'>".$value['city']."</option>";
            }
        }
        $data['data'] = $li;
        return json($data);

    }

    function send_quan(){
        if ($this->request->isPost()) {// 数据验证
            $list = $this->request->post();
            $list['ctime'] = time();
            if($list['quan_id']>0&&$list['send_num']>0){
                $quan_info = Db::name('product_quan')->where('id',$list['quan_id'])->find();
                if($quan_info['rest_num']<$list['send_num']){
                    return $this->error('优惠券库存不足，目前库存为'.$quan_info['rest_num']);
                }
                $in_data = array(
                    'uid'=>$list['uid'],
                    'quan_id'=>$list['quan_id'],
                    'status'=>0,
                    'ctime'=>time()
                );
                $num = $list['send_num'];
                while($num>0){
                    Db::name('user_quan')->insert($in_data);
                    $num -= 1;
                }
                Db::name('product_quan')->where('id',$list['quan_id'])->setDec('rest_num',$list['send_num']);

                return $this->success('操作成功');
            }
        }

        $time = time();
        // $gwhere[] = ['use_end_time','gt',$time];
        $gwhere[] = ['rest_num','gt',0];
        $groups = Db::name('product_quan')->where($gwhere)->column('id,quan_name');
        $this->assign('groups', $groups);

        $id = input('id/d');
        // $list = Db::name('product_in_record')->where('pid',$id)->order('ctime desc')->select();
        $this->assign('uid',$id);
        $template = $this->request->param('template', 'add_quan_form');
        return $this->fetch($template);
    }

    function getList(){
        $id = input('id');
        $where[] = ['a.uid','eq',$id];
        $list = Db::name('user_quan')->alias('a')->join('product_quan b','a.quan_id=b.id','left')->where($where)->order('a.ctime desc')->field('a.*,b.quan_name,b.quan_desc')->select();
        if($list){
            foreach($list as &$value){
                $value['ctime'] = $value['ctime']?date('Y-m-d H:i:s',$value['ctime']):'';
                if($value['status']==0){
                    $value['status'] = '未使用';
                }
                else{
                    $value['status'] = '已使用';
                }
            }
        }

        $data['data'] = $list;
        $data['count'] = Db::name('user_quan')->where('uid',$id)->count('id');
        $data['code'] = 0;
        return json($data);
    }

    function send_jifen(){
        if ($this->request->isPost()) {// 数据验证
            $list = $this->request->post();
            $list['ctime'] = time();
            if($list['send_num']>0){
                
                $in_data = array(
                    'uid'=>$list['uid'],
                    'jifen'=>$list['send_num'],
                    'ctime'=>time(),
                    'year'=>date('Y',time()),
                    'month'=>date('m',time()),
                    'day'=>date('Y-m-d',time()),
                    'type'=>1,
                    'from_type'=>2
                );
                Db::name('user_jifen')->insert($in_data);
                Db::name('user')->where('id',$list['uid'])->setInc('jifen',$list['send_num']);
                // Db::name('product_quan')->where('id',$list['quan_id'])->setDec('rest_num',$list['send_num']);

                return $this->success('操作成功');
            }
            elseif($list['send_num']<0){
                $in_data = array(
                    'uid'=>$list['uid'],
                    'jifen'=>$list['send_num'],
                    'ctime'=>time(),
                    'year'=>date('Y',time()),
                    'month'=>date('m',time()),
                    'day'=>date('Y-m-d',time()),
                    'type'=>1,
                    'from_type'=>2
                );
                Db::name('user_jifen')->insert($in_data);
                Db::name('user')->where('id',$list['uid'])->setDec('jifen',abs($list['send_num']));
            }
        }

        $time = time();
        $id = input('id/d');
        $jifen = Db::name('user')->where('id',$id)->value('jifen');
        $this->assign('jifen',$jifen);
        // $list = Db::name('product_in_record')->where('pid',$id)->order('ctime desc')->select();
        $this->assign('uid',$id);
        $template = $this->request->param('template', 'add_jifen_form');
        return $this->fetch($template);
    }

    function getJifenList(){
        $id = input('id');
        $page       = $this->request->param('page/d', 1);
        $limit      = $this->request->param('limit/d', 15);
        $where[] = ['uid','eq',$id];
        $list = Db::name('user_jifen')->where($where)->page($page)->limit($limit)->order('ctime desc')->select();
        if($list){
            foreach($list as &$value){
                $value['ctime'] = $value['ctime']?date('Y-m-d H:i:s',$value['ctime']):'';
                if($value['type']==1){
                    $value['type'] = '收入';
                }
                else{
                    $value['type'] = '支出';
                    $value['jifen'] = $value['jifen']*-1;
                }

                if($value['from_type']==0){
                    $value['from_type']='购买产品获得积分';
                }
                elseif($value['from_type']==1){
                    $value['from_type']='分销产品获得积分';
                }
                elseif($value['from_type']==2){
                    $value['from_type']='后台添加积分';
                }
            }
        }

        $data['data'] = $list;
        $data['count'] = Db::name('user_quan')->where('uid',$id)->count('id');
        $data['code'] = 0;
        return json($data);
    }
}

?>